package com.wing.api.query.device.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSONObject;
import com.wing.api.ApplicationContextUtil;
import com.wing.api.query.device.BaseQueryDeviceService;
import com.wing.cabinet.model.response.EnterpricceIdVO;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.enterprise.device.entity.Device;
import com.wing.enterprise.feign.EnterpriseClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 标准设备查询接口
 */
@Service
@Slf4j
public class StandardQueryDeviceServiceImpl implements BaseQueryDeviceService {

    @Override
    public PageResult<Device> queryList(JSONObject jsonObject, EnterpricceIdVO enterpricceIdVo, EnterpriseClient enterpriseClient) {
        log.info("请求参数,{}", jsonObject.toJSONString());
        //此处需要做接口签名验证
        //sign = md5（参数1+参数2+参数3+key）
        String sourceSign = jsonObject.getString("sign"); // 签名值
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String name = jsonObject.getString("name");
        String deviceCode = jsonObject.getString("deviceCode");

        if(StringUtils.isBlank(sourceSign)){
            throw new BusinessException("签名值为空！");
        }

        if(page == null || page == 0){
            throw new BusinessException("page参数传值异常！");
        }
        if(limit == null || limit == 0){
            throw new BusinessException("limit参数传值异常！");
        }

        StringBuffer tempStr = new StringBuffer();
        tempStr.append(page);
        tempStr.append(limit);
        if(StringUtils.isNotBlank(name)){
            tempStr.append(name);
        }
        if(StringUtils.isNotBlank(deviceCode)){
            tempStr.append(deviceCode);
        }

        tempStr.append(enterpricceIdVo.getKey());
        log.info("签名源串,{}", sourceSign);
        String desSign = DigestUtil.md5Hex(tempStr.toString());
        log.info("签名目的串,{}", desSign);
        if(!desSign.equals(sourceSign)){
            throw new BusinessException("签名不通过！");
        }
        return enterpriseClient.getDevicePageList(page, limit, null, null, name, deviceCode);
    }

    @Override
    public JsonResult queryDetail(Long id, EnterpricceIdVO enterpricceIdVo, String sign) {
        log.info("请求参数,id="+id+",sign="+sign);
        //此处需要做接口签名验证
        //sign = md5（参数1+参数2+参数3+key）

        if(StringUtils.isBlank(sign)){
            throw new BusinessException("签名值为空！");
        }

        StringBuffer tempStr = new StringBuffer();
        tempStr.append(id);
        tempStr.append(enterpricceIdVo.getKey());
        log.info("签名源串,{}", tempStr.toString());
        String desSign = DigestUtil.md5Hex(tempStr.toString());
        log.info("签名目的串,{}", desSign);
        if(!desSign.equals(sign)){
            throw new BusinessException("签名不通过！");
        }
        EnterpriseClient enterpriseClient = (EnterpriseClient) ApplicationContextUtil.getBean("enterpriseClientFallback");

        return enterpriseClient.foreignProductInfoDetail(id);
    }

    public static void main(String[] args) {
//        String key = "e10adc3949ba59abbe56e057f20f883e";
        String key = "$2a$10$fxpgo/SLFHCeNThnfF/q7uvoZJOxbJBNNI4.dshzlntgBIHKI9eb."; // 测试
//        String key = "$2a$10$A4q7sKe4/3GekaIIHLTqG.Gd4y8PhX4yLNleuHK9fvvyWq1vodM6e";
        Integer page = 1;
        Integer limit = 10;

        StringBuffer tempStr = new StringBuffer();
        tempStr.append(page);
        tempStr.append(limit);

        tempStr.append(key);
//        log.info("签名源串,{}", sourceSign);
        String desSign = DigestUtil.md5Hex(tempStr.toString());
//        log.info("签名目的串,{}", desSign);
        System.out.println(desSign);
    }

}
